"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : order by,按照中文拼音排序
Create At   : 2022/12/08
Owner       : opentestcase034
Description :
            1、创建编码为utf8的库
            2、创建表
            3、在表中插入数据
            4、查询表数据并将结果插入到新表
            5、查询新表
            6、清理环境
Expect      :
            1、成功
            2、成功
            3、成功
            4、成功
            5、成功
            6、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.db_name = 'db_select_into_0025'
        self.t_name1 = 't_select_into_0025_1'
        self.t_name2 = 't_select_into_0025_2'

    def test_order_by(self):
        step1 = '--step1:创建编码为utf8的库;expect:成功--'
        self.log.info(step1)
        sql_cmd = f'''
                    drop database if exists {self.db_name};
                    create database {self.db_name} encoding 'utf8';
                    '''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.DROP_DATABASE_SUCCESS, result,
                      '执行失败' + step1)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS, result,
                      "执行失败" + step1)

        step2 = '--step2:创建表;expect:成功--'
        self.log.info(step2)
        sql_cmd = f'''
                    drop table if exists {self.t_name1};
                    create table {self.t_name1}(id int,c_clob clob);
                    '''
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                    dbname=self.db_name)
        self.log.info(result)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, result,
                      '执行失败' + step2)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, result,
                      "执行失败" + step2)

        step3 = '--step3:在表中插入数据;expect:成功--'
        self.log.info(step3)
        sql_cmd = f'''
                    insert into {self.t_name1} values(1,'和');
                    insert into {self.t_name1} values(2,'给');
                    insert into {self.t_name1} values(3,'发');
                    insert into {self.t_name1} values(4,'额');
                    insert into {self.t_name1} values(5,'的');
                    insert into {self.t_name1} values(6,'从');
                    insert into {self.t_name1} values(7,'吧');
                    insert into {self.t_name1} values(8,'啊');
                    '''
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                    dbname=self.db_name)
        self.log.info(result)
        self.assertEqual(8, result.count(self.Constant.INSERT_SUCCESS_MSG),
                         '执行失败'+step3)

        step4 = '--step4:查询表数据并将结果插入到新表;expect:成功--'
        self.log.info(step4)
        sql_cmd = f'''
                    select * into {self.t_name2} 
                    from {self.t_name1} order by nlssort 
                    (c_clob, 'nls_sort = schinese_pinyin_m');
                    '''
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                    dbname=self.db_name)
        self.log.info(result)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, result, '执行失败' + step4)

        step5 = '--step5:查询新表;expect:成功--'
        self.log.info(step5)
        sql_cmd = f'''select * from {self.t_name2};'''
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                    dbname=self.db_name)
        self.log.info(result)
        exp = ['啊', '吧', '从', '的', '额', '发', '给', '和']
        for i in range(8):
            self.assertIn(exp[i], result.splitlines()[2:-1][i], '执行失败'+step5)

    def tearDown(self):
        step6 = '--step6:清理环境;expect:成功--'
        self.log.info(step6)
        sql_cmd = f'''
                    drop database if exists {self.db_name};
                    '''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.DROP_DATABASE_SUCCESS, result, '执行失败'+step6)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')